import Creator from './tree_data';
import { OrganizationConst } from "src/consts/organization_const";
import { addTreeChildren, findTreeNode } from "src/utils/tree_utils";
import { PopupName } from "src/consts/popup_const";
import * as Api from 'src/api/net';
import Vue from 'vue'

const state: any = Creator.dataCreator();

state.curr_page = 1;
state.page_size = 15;
state.level = 0;
state.data_list = []
state.type = "";
state.isShow = false
// vuex

/** 更新数据的 */
const mutations = {
    [OrganizationConst.AddTreeNode](state: any, data: any): void {
        let row: any = findTreeNode(state.data_list, data.id);
        console.log("org row", state.data_list, "org data", row, "org data", data);
        if (row) {
            addTreeChildren(row, data.id, data.data);
        } else {
        }
    },

    /** 初始化列表 */
    [OrganizationConst.UpdateOrgTreeData](state: any, data: any): void {
        state.curr_page = data.curr_page;
        state.page_size = data.page_size;
        state.total_page = data.total_page;
        state.total_size = data.total_size;
        let len = data.data_list.length;
        for (let i = 0; i < len; i++) {
            let tmp: any = data.data_list[i];
            tmp.selected = false;
        }
        state.data_list = data.data_list;

        if (state.type == "acc") {
            WLLib.panel(PopupName.Acc_Org_Panel);
            state.type = ""
        }

        if (state.isShow) {
            for (let k = 0; k < len; k++) {
                Vue.set(data.data_list[k], '_expanded', true)
                let sendData: any = {}
                sendData["curr_page"] = WLStore().state.org.curr_page
                sendData["page_size"] = WLStore().state.org.page_size
                sendData.ind = data.data_list[k].id
                if (data.data_list[k].parent) {
                    sendData.parent = data.data_list[k].id
                } else {
                    sendData.parent = data.data_list[k].child_list[0].parent
                }

                WLDispatch(OrganizationConst.ExpandTree, sendData);
            }
        }
    },

    [OrganizationConst.CommitType](state: any, data: String): void {
        state.type = data
    },

    [OrganizationConst.CommitQuery](state: any, data: Boolean): void {
        state.isShow = data
    }
}

//异步处理
const actions = {
    [OrganizationConst.GetOrgTreeData](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/dept/list.do", data, (res: any) => {
            WLCommit(OrganizationConst.UpdateOrgTreeData, res.data);
        });
    },
    [OrganizationConst.ExpandTree](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/dept/list.do", data, (res: any) => {
            var param: any = {};
            param.id = data.ind;
            param.data = res.data.data_list;
            WLCommit(OrganizationConst.AddTreeNode, param);
        });
    },
    [OrganizationConst.DeleteTreeNode](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/dept/del.do", data, (res: any) => {
            WLMessage("删除部门成功", "success")

            let senData: any = {}
            senData["curr_page"] = state.curr_page
            senData["page_size"] = state.page_size
            senData.level = 0
            WLDispatch(OrganizationConst.GetOrgTreeData, senData);
        });
    },

    [OrganizationConst.DispatchOrgAdd](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/dept/add.do", data, (res: any) => {
            WLMessage("新增部门成功", "success")

            let senData: any = {}
            senData["curr_page"] = state.curr_page
            senData["page_size"] = state.page_size
            senData.level = 0
            WLDispatch(OrganizationConst.GetOrgTreeData, senData);

            if (!data.isContinue) {
                WLLib.panel(PopupName.Org_Add_Panel).close();
            }
        });
    },
}


export default {
    state,
    mutations,
    actions
}